// 页面路径：store/index.js 
import Vue from 'vue'
import Vuex from 'vuex'

// 导入 user用户 模块
import user from './user'

Vue.use(Vuex);//vue的插件机制

//Vuex.Store 构造器选项
const store = new Vuex.Store({
	state:{//存放状态
		cartList: uni.getStorageSync('cartList') || [],
	},
  mutations: {
    // 更新 state 中的购物车数组
    updateCartList(state, params) {
      state.cartList = params
      uni.setStorageSync('cartList', params)
    }
  },
  getters: {
    // 全选框状态
    isAllSelected(state) {
      return state.cartList.every(item => item.goods_select)
    },
    // 筛选出被选中的商品 
    selectedCartList(state) {
      return state.cartList.filter(item => item.goods_select)
    },
    // 计算被选中的商品数量 
    selectedCartListCount(state, getters) {
      return getters.selectedCartList.reduce((sum, item) => sum += item.goods_count, 0)
    },
    // 计算被选中的商品总价
    selectedCartListPrice(state, getters) {
      return getters.selectedCartList.reduce((sum, item) => sum += item.goods_count * item.goods_price, 0)
    }
  },
  modules: {
    // user 用户模块
    user
  }
})
export default store